//
//  LYMemorialView.m
//  SZBank
//
//  Created by LuoYao on 16/7/12.
//  Copyright © 2016年 LuoYao. All rights reserved.
//  备忘录

#import "LYMemorialView.h"

@interface LYMemorialView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *_nowTime;/**< 当前时间，用于存储备忘录 */
    UITextView *_detail;/**< 备忘录输入内容 */
    NSArray *_dataArrayList;/**< 备忘录列表 */
    NSInteger _index;/**< 当前选中的数据 */
}
@property (nonatomic) UITableView *list;/**< 备忘录显示列表 */
@end
@implementation LYMemorialView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initDataSource];
        
        //背景
        self.backImageView.image = [LocalDataManager getImageWithSourceOfPath:@"备忘录_05"];


        //输入框
        UITextField *searchField = [[UITextField alloc] initWithFrame:CGRectMake(_cKwidth * 0.01, _cKheight * 0.02, _cKwidth * 0.35, 50)];
        [searchField setBackground:[LocalDataManager getImageWithSourceOfPath:@"备忘录_搜索"]];
        
        [self addSubview:searchField];
        
        
        //显示列表
        [self addSubview:self.list];
        
        
        //右侧label
        
        //获取当前日期
        NSDateFormatter *formater = [[NSDateFormatter alloc] init];
        [formater setTimeZone:[NSTimeZone localTimeZone]];
        [formater setDateFormat:@"yyyy-MM-dd"];
        
        NSDate *nowDate = [NSDate date];
        NSString *date = [formater stringFromDate:nowDate];
        _nowTime = date;
        
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(_cKwidth * 0.4, _cKheight * 0.05, _cKwidth * 0.2, 50)];
        dateLabel.text = date;
        dateLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        [self addSubview:dateLabel];
        
        //右侧Btn
        NSArray *btnTitle = @[@"备忘录_增加",@"备忘录_删除"];
        for (int i = 0; i < 2; i ++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(_cKwidth * 0.7 + i * _cKwidth * 0.15 , _cKheight * 0.05, _cKwidth * 0.12, 40)];
            btn.tag = 1000 + i;
            [btn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:btnTitle[i]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
        }
        
        //右侧textView
        _detail = [[UITextView alloc] initWithFrame:CGRectMake(_cKwidth * 0.4, _cKheight * 0.12, _cKwidth * 0.59, _cKheight * 0.86)];
        _detail.font = [UIFont systemFontOfSize:FONT_SIZE];
        
        [self addSubview:_detail];
        
        
    }
    return self;
}


/**< 初始化数据源 */
- (void)initDataSource{
   _dataArrayList = [LocalDataManager getMemorandumWithPlist];
}


#pragma mark - 点击事件
- (void)action_btnClick:(UIButton *)sender{
    switch (sender.tag - 1000) {
        case 0:
        {
            //获取输入的数据
            NSString *inputText = _detail.text;
            NSString *nowTime = _nowTime;
            
            if (inputText.length == 0) {
                return;
            }
            
            NSDictionary *dic = @{@"time":nowTime,@"text":inputText};
            
            //调用方法进行存储
            [LocalDataManager saveMemorandumWithDictionary:dic];
            
            //清空编辑
            _detail.text = @"";
        }
            break;
            
            
        case 1:
        {
            //获取当前index调用方法进行删除
            [LocalDataManager removeMemorandumWithIndex:_index];
            
        }
            break;
            
        default:
            break;
    }
    //更新数据
    [self initDataSource];
    [self.list reloadData];
}


#pragma mark - uitableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArrayList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",_dataArrayList[indexPath.row][@"time"],_dataArrayList[indexPath.row][@"text"]];
    cell.textLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //选中
    _detail.text = _dataArrayList[indexPath.row][@"text"];
    
    //index
    _index = indexPath.row;
}



#pragma mark - getter
- (UITableView *)list{
    if (!_list) {
        _list = [[UITableView alloc] initWithFrame:CGRectMake(_cKwidth * 0.01, _cKheight * 0.12, _cKwidth * 0.35, _cKheight * 0.86)];
        _list.delegate = self;
        _list.dataSource= self;
        
        [_list registerClass:[UITableViewCell class] forCellReuseIdentifier:@"listcell"];
    }
    return _list;
}



@end
